package com.ruoyi.web.controller.zhuangxiucontroller.shopController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.ProductDetailsImage;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.ProductInfo;
import com.ruoyi.system.service.zhaungxiuservice.shopService.ProductDetailsImgService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shopping/productDetails")
public class ProductDetailsImgController {
    @Autowired
    private ProductDetailsImgService productDetailsImgService;

    /**
     * 添加商品介绍图片
     * @param productDetailsImage
     * @return
     */
    @ApiResponses({@ApiResponse(code = 200, message = "正常", response = ProductDetailsImage.class)})
    @ApiOperation(value = "新增商品介绍多图片")
    @PreAuthorize("@ss.hasPermi('shopping:goods:addpi')")
    @Log(title = "新增商品介绍图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addProductDetailsImg(@RequestBody ProductDetailsImage productDetailsImage)
    {
        if(productDetailsImage.getProductDetailsUrls()!=null && productDetailsImage.getProductDetailsUrls().size()>0) {
            int count = 0;
            for (String productDetailsUrl : productDetailsImage.getProductDetailsUrls()) {
                productDetailsImage.setProductDetailsUrl(productDetailsUrl);
                count = productDetailsImgService.addProductInfoPic(productDetailsImage);
            }
            if(count == 0){
                return AjaxResult.error();
            }else {
                return AjaxResult.success(count);
            }
        }else {
            return AjaxResult.error();
        }

    }

    @PreAuthorize("@ss.hasPermi('shopping:master:listppi')")
    @GetMapping("/listDetailsImg/{productId}")
    public AjaxResult listDetailsImg(@PathVariable(value = "productId", required = false) Long productId)
    {
        List<ProductDetailsImage> list= productDetailsImgService.getProductInfoPicById(productId);
        return AjaxResult.success(productDetailsImgService.getProductInfoPicById(productId));
    }

    /**
     *
     * @param picName
     * @return
     */
    @ApiResponses({@ApiResponse(code = 200, message = "正常", response = ProductInfo.class)})
    @ApiOperation(value = "删除商品详情图片信息")
    @PreAuthorize("@ss.hasPermi('shopping:goods:removepi')")
    @Log(title = "删除商品详情图片信息", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteDetailsImgByName")
    public AjaxResult deleteDetailsImgByName( @RequestBody String picName){
        int count = productDetailsImgService.deleteProdoctInfoPic(picName);
        if(count>0){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

}
